package com.shi.yibanjia.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.StaticLog;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.service.SMSService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.shi.yibanjia.common.constant.RedisConstant.*;

@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "dev")
@Service
public class TestSMSServiceImpl implements SMSService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result sendSMS(String phone) {
        // 1. 生成6位随机数验证码
        String randomNumbers = RandomUtil.randomNumbers(6);
        StaticLog.info("手机号验证码: {}", randomNumbers);
        // 2. 手机号作为key，将验证码存入redis
        stringRedisTemplate.opsForValue().set(PHONE_CODE + phone, randomNumbers, PHONE_CODE_TTL, TIME_UNIT);
        return Result.ok();
    }
}
